module MC {
	export module Call {
		export class MsgWin {
			public constructor() {
				this.init();
			}
			/** 最新的最大id */
			private maxid = 0;
			/** 当前对象的用户id */
			private uid: any = 0;
			private init() {
				this.uid = MC.get('uid');
				$('.setJobSend').click(() => {
					this.setJobSend();
				});
				this.upmsg();
				setInterval(() => {
					this.upmsg();
				},15000);
				MC.user.getInfo(this.uid, (data: any) => {
					//console.log(data);
					$('#titleName').text(data.name);
				});
			}
			private setJobSend() {
				MC.click(() => {

					let quest = $('#quest').val();
					if (quest == '') {
						MC.msg.alert("必须的输入内如");
						return;
					}
					if (quest.length > 140) {
						MC.msg.alert("输入的内容太多了");
						return;
					}
					$('#quest').val('');
					MC.api('CallSendMsg', {
						uid: this.uid,
						quest: quest,
						maxid: this.maxid
					}, (json: any) => {
						this.addli(json);
					});
				});
			}
			/**
			 * 刷新消息
			 */
			private upmsg() {
				MC.api('getCallMsg', {
					uid: this.uid,
					maxid: this.maxid
				}, (json: any) => {
					this.addli(json);
					
				});
			}

			private addli(json: any) {
				this.maxid = json.maxid;
				let newlist = json.newlist;
				for (let i = newlist.length-1; i >=0; i--) {
					let one = newlist[i];
					let li:any;
					if (one.isme) {
						li = $(`<div class="question">
							<div class="heard_img right">
								<img src="`+ one.user.head + `" height="40"/>
							</div>
							<div class="question_text">
								<p>`+ one.content + `</p>
							<i></i>
						</div>
						</div>`);
					} else {
						li =$(`<div class="answer">
							<div class="heard_img left">
								<img src="`+ one.user.head + `" height="40"/>
							</div>
							<div class="answer_text">
								<p>`+ one.content + `</p>
								<i></i>
						</div>
					</div>`);
					}
					li.attr('msgid',one.id);
					if(!$('[msgid='+one.id+']')[0]){
						$('#scrollDiv').append(li);
					}
				}
				//console.log(newlist);
				this.buttom();
			}


			/** 滚动到底部 */
			private buttom(){
					$(".speak_window").animate({scrollTop:$(".speak_window").prop("scrollHeight")}, 400);
			}

		}

	}

}